﻿
Partial Class pages_files_Missing : Inherits CPage

#Region "Querystring (Filters)"
    Public ReadOnly Property Search As String
        Get
            Return CWeb.RequestStr("search")
        End Get
    End Property

    'Rename or Delete:
    Public ReadOnly Property ExtensionId As Integer
        Get
            Return CWeb.RequestInt("extensionId")
        End Get
    End Property
    Public ReadOnly Property ExtensionIdAsByte As Byte?
        Get
            If Integer.MinValue = ExtensionId Then Return Nothing
            Return ExtensionId '0 for no value set, int.min for all
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [Files]() As CFileList
        Get
            'Paging done in usercontrol
            Dim list As New CFileList()
            For Each i As CFile In CFile.Cache.Search(Nothing, ExtensionIdAsByte)
                Dim path As String = CConfig.DropboxPublic & i.FileName
                If Not IO.File.Exists(path) Then
                    list.Add(i)
                ElseIf i.FileSize <= 0 Then
                    Try
                        i.FileSize = New IO.FileInfo(path).Length
                        i.Save()
                        lit.Text = String.Concat(lit.Text, "<br/>", CUtilities.FileSize(i.FileSize), " ~ ", i.FileName)
                    Catch
                    End Try
                End If
            Next
            Return list
        End Get
    End Property
#End Region

#Region "Event Handlers - Page "
    Protected Overrides Sub PageInit()
        'Populate Dropdowns
        AddMenuSide("Files", CSitemap.Files(Nothing, ExtensionId))
        AddMenuSide("Duplicates", CSitemap.Duplicates)
        AddMenuSide("Missing")

        ddExt.DataSource = CExtension.Cache
        ddExt.DataBind()
        CDropdown.BlankItem(ddExt, "-- All Extensions --")
        CDropdown.BlankItem(ddExt, "* None Set ", 0)
        CDropdown.SetValue(ddExt, ExtensionId)

        'Display Results
        ctrlFiles.Display(Me.Files, True)

    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddExt.SelectedIndexChanged
        Response.Redirect(CSitemap.FilesMissing(CDropdown.GetInt(ddExt)))
    End Sub
#End Region

#Region "Event Handlers - UserControl"
    Private Sub ctrl_ExportClick() Handles ctrlFiles.ExportClick
        Files.ExportToCsv(Response, "Files.csv")
    End Sub
    Private Sub ctrl_ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer) Handles ctrlFiles.ResortClick
        Response.Redirect(CSitemap.FilesMissing(ExtensionId, New CPagingInfo(0, pageNumber - 1, sortBy, descending)))
    End Sub
#End Region

End Class
